package com.yingpai.fitness.dvp_2_0.main.fragment.mine.integral;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.LogisticsActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.IsSuccessBean;
import com.yingpai.fitness.dvp_2_0.main.bean.integral.IntegralOrderListBean;
import com.yingpai.fitness.dvp_2_0.main.util.DialogUtil;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.wx.TokenBean;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ASUS on 2018/12/25.
 */

public class IntegralOrderListFragment extends CommonFragment implements OnRefreshLoadMoreListener {
    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    LinearLayout empty;
    @BindView(R.id.title_bar)
    RelativeLayout title_bar;
    Unbinder unbinder;
    private String status;
    private TokenBean tokenBean;
    private int pageNumber = 1;
    private int pageSize = 10;
    private QuickAdapter<IntegralOrderListBean.DataBean.ListBean> quickAdapter;

    public static IntegralOrderListFragment newInstance(String status) {
        IntegralOrderListFragment integralOrderListFragment = new IntegralOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", status);
        integralOrderListFragment.setArguments(bundle);
        return integralOrderListFragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dvp_2_0_fragment_order_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        title_bar.setVisibility(View.GONE);
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));
        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        status = getArguments().getString("status");

        quickAdapter = new QuickAdapter<IntegralOrderListBean.DataBean.ListBean>(context, R.layout.item_integral_order_list) {
            @Override
            protected void convert(BaseAdapterHelper helper, final IntegralOrderListBean.DataBean.ListBean item) {
//                helper.getView(R.id.item_ll).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Bundle bundle = new Bundle();
//                        bundle.putString("id",item.getId());
//                        startActivity(IntegralMallShopDetailActivity.class,bundle);
//                    }
//                });
                helper.setText(R.id.title_tv, item.getBrandName());
                helper.setText(R.id.order, "订单号：" + item.getOrderCode());
                helper.setText(R.id.shop_count, "共" + item.getSl() + "件商品");
                helper.setText(R.id.money, item.getExchangePoint() + "");
                helper.setText(R.id.title, item.getName());
                helper.setText(R.id.integral, item.getExchangePoint() + "");
                ImageView photo = helper.getView(R.id.photo);
                if(!TextUtils.isEmpty(item.getPhotoUrl())){
                    Glide.with(context)
                            .load(item.getPhotoUrl().contains(",")?item.getPhotoUrl().split(",")[0]:item.getPhotoUrl())
                            .error(R.mipmap.default_small_bg)
                            .into(photo);
                }

                TextView left_tv = helper.getView(R.id.left_tv);
                TextView right_tv = helper.getView(R.id.right_tv);
                /**
                 * dictVal = 3商品
                 */
                if (item.getDictVal().equals("3")) {
                    if (item.getStatus().equals("1") || item.getStatus().equals("7") || item.getStatus().equals("2")) {
                        left_tv.setVisibility(View.VISIBLE);

                        left_tv.setText("物流信息");
                        right_tv.setText("确认收货");
                        if(item.getStatus().equals("7")){
                            right_tv.setVisibility(View.GONE);
                        }else{
                            right_tv.setVisibility(View.VISIBLE);
                        }


                        if (item.getStatus().equals("2")) {
                            left_tv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("com", item.getExprCompCode());
                                    bundle.putString("num", item.getExprCode());
                                    bundle.putString("state", item.getStatus());
                                    startActivity(LogisticsActivity.class, bundle);
                                }
                            });
                            right_tv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().showCenterDialog(IntegralOrderListFragment.this.context, R.layout.dialog_shop_pay_success);
                                    View dialog_layout = DialogUtil.getInstance().dialog_center_layout;

                                    TextView title = dialog_layout.findViewById(R.id.title);
                                    title.setText("确定已收到商品？");
                                    TextView message = dialog_layout.findViewById(R.id.message);
                                    message.setVisibility(View.GONE);
                                    dialog_layout.findViewById(R.id.sure_cancel).setVisibility(View.VISIBLE);

                                    dialog_layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            DialogUtil.getInstance().dialog_center.dismiss();
                                        }
                                    });

                                    dialog_layout.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            DialogUtil.getInstance().dialog_center.dismiss();
                                            showDialog(IntegralOrderListFragment.this.context, null);
                                            HashMap<String, Object> httpParams = new HashMap();
                                            httpParams.put("OrderId", item.getId());
                                            finalOkGo.requestEncrypt(RequestType.POSTENCRYPT,
                                                    Contacts.HOST_SERVER + Url.INTEGRAL_CONFIRM_RECEIVE,
                                                    httpParams, Url.INTEGRAL_CONFIRM_RECEIVE, new Callback<IsSuccessBean>() {
                                                        @Override
                                                        public void onSuccess(IsSuccessBean baseResponse) {
                                                            dismissDialog();
                                                            if (baseResponse.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
//                                                quickAdapter.remove(helper.getPosition());
//                                                quickAdapter.notifyDataSetChanged();
                                                                onRefresh(refreshLayout);
                                                            } else {
                                                                showToast(baseResponse.getMessage() + "");
                                                            }
                                                        }
                                                    });
                                        }
                                    });
                                    dialog_layout.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            DialogUtil.getInstance().dialog_center.dismiss();
                                        }
                                    });
                                }
                            });
                        } else {
                            left_tv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("com", item.getExprCompCode());
                                    bundle.putString("num", item.getExprCode());
                                    bundle.putString("state", item.getStatus());
                                    startActivity(LogisticsActivity.class, bundle);
                                }
                            });
                            right_tv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    showToast("正在出货...");
                                }
                            });
                        }
                    } else if (item.getStatus().equals("3")) {
                        left_tv.setVisibility(View.VISIBLE);
                        right_tv.setVisibility(View.GONE);
                        left_tv.setText("物流信息");
                        left_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Bundle bundle = new Bundle();
                                bundle.putString("com", item.getExprCompCode());
                                bundle.putString("num", item.getExprCode());
                                bundle.putString("state", item.getStatus());
                                startActivity(LogisticsActivity.class, bundle);
                            }
                        });
                    } else if (item.getStatus().equals("6")) {
                        left_tv.setVisibility(View.GONE);
                        right_tv.setVisibility(View.GONE);
//                        left_tv.setText("查看");
//                        left_tv.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                Bundle bundle = new Bundle();
//                                bundle.putString("id",item.getId());
//                                startActivity(IntegralMallShopDetailActivity.class,bundle);
//                            }
//                        });
                    }
                } else {
                    left_tv.setVisibility(View.GONE);
                    right_tv.setVisibility(View.GONE);
                }


            }
        };
        listView.setAdapter(quickAdapter);
        return view;
    }

    @Override
    protected void initData() {
        showDialog(context, null);
        HashMap<String, Object> params = new HashMap();
        params.put("pageNumber", pageNumber++);
        params.put("pageSize", pageSize);
        params.put("type", status);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.GET_INTEGRAL_ORDER_LIST, params,
                Url.GET_INTEGRAL_ORDER_LIST, new Callback<IntegralOrderListBean>() {
            @Override
            public void onSuccess(IntegralOrderListBean integralOrderListBean) {
                if (integralOrderListBean.getCode().equals(Constant.REQUEST_SUCCESS_0) && integralOrderListBean.getData() != null && integralOrderListBean.getData().getList() != null && integralOrderListBean.getData().getList().size() != 0) {
                    refreshLayout.setNoMoreData(false);
                    if (pageNumber - 1 == 1) {
                        quickAdapter.clear();
                        for (int i = 0; i < integralOrderListBean.getData().getList().size(); i++) {
                            if (integralOrderListBean.getData().getList().get(i).getStatus().equals("1")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("2")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("3")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("6")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("7")) {
                                quickAdapter.add(integralOrderListBean.getData().getList().get(i));
                            }
                        }
                        if(quickAdapter.getCount()!=0){
                            empty.setVisibility(View.GONE);
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        for (int i = 0; i < integralOrderListBean.getData().getList().size(); i++) {
                            if (integralOrderListBean.getData().getList().get(i).getStatus().equals("1")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("2")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("3")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("6")
                                    || integralOrderListBean.getData().getList().get(i).getStatus().equals("7")) {
                                quickAdapter.add(integralOrderListBean.getData().getList().get(i));
                            }
                        }
                    }

                } else {
                    if (pageNumber - 1 == 1) {
                        quickAdapter.clear();
                        empty.setVisibility(View.VISIBLE);
                    }else{
                        empty.setVisibility(View.GONE);
                    }
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
                quickAdapter.notifyDataSetChanged();
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
                if (pageNumber - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                }else{
                    empty.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    @OnClick({R.id.empty})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.empty:
                onRefresh(refreshLayout);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNumber = 1;
        initData();
    }
}
